#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <unistd.h>

using namespace std;

struct Record
{
	string _word;
	int _frequency;
    Record(string word, int frequency):_word(word), _frequency(frequency){}
};

class Dictionary
{
public:
    Dictionary():_dict(){
        _dict.reserve(20000);
    }
    void read(const string &filename){
        ifstream fd(filename);
        if(!fd.good()){
            cerr<<"ropen fail"<<endl;
            return;
        }
        string word;
        while(fd >> word){
            bool found = 0;
            for(auto &x: _dict){
                if(x._word == word){
                    ++x._frequency;
                    found = 1;
                    break;
                }
            }
            if(!found){
                _dict.emplace_back(word, 1);
            }
        }
        fd.close();

    }
    void store(const string &filename){
        ofstream fd(filename);
        if(!fd.good()){
            cerr<<"wopen fail"<<endl;
        }
        //ostringstream entry;
        for(auto &x: _dict){
            fd<<x._word<<" : "<<x._frequency<<endl;
            //entry<<x._word<<" : "<<x._frequency<<endl;
            //fd<<entry.str();
            //entry.str("");
        }
        fd.close();
    }
    void print(){
        for(auto &x: _dict){
            cout<<x._word<<" : "<<x._frequency<<endl;
        }
    }
private:
    vector<Record> _dict;
};  

int main()
{
    string readFile("/home/snower/me/study/code/C_Pro_Learn/c++/files/The_Holy_Bible.txt");
    //string readFile("test.txt");
    string outFile("Holy_Dictionary.txt");
    Dictionary Holy_Dictionary;
    Holy_Dictionary.read(readFile);
    //Holy_Dictionary.print();
    Holy_Dictionary.store(outFile);

    return 0;
}

